<template>
  <div class="app-container">
    <el-card>
      <el-form ref="searchForm" :model="searchForm" inline size="small">
        <el-form-item label="标题">
          <el-input
            v-model="searchForm.search"
            placeholder="请输入标题名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            clearable
            v-model="searchForm.enableStatus"
            placeholder="请选择"
          >
            <el-option label="上架" value="1"></el-option>
            <el-option label="下架" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布平台">
          <el-select
            v-model="searchForm.platformType"
            placeholder="请选择发布平台"
            clearable
            size="small"
          >
          <el-option
              v-for="item in platformTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" @click="resetForm('searchForm')"
            >重置</el-button
          >
          <el-button icon="el-icon-search" type="primary" @click="getList()"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div>
        <el-button
          size="small"
          type="primary"
          @click="addArtice()"
          icon="el-icon-plus"
          >添加专题</el-button
        >
      </div>

      <el-table
        :data="tableData.data"
        v-loading="listLoading"
        border
        style="width: 100%; margin-top: 20px"
        size="small"
      >
        <el-table-column align="center" type="index" label="编号" width="80" />
        <el-table-column
          align="center"
          prop="title"
          label="标题"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>《 {{ scope.row.title }} 》</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="subTitle"
          label="副标题"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>《 {{ scope.row.subTitle }} 》</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="封面">
          <template slot-scope="scope">
            <img
              :src="scope.row.pic"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column
          align="center"
          prop="label"
          label="标签"
          :show-overflow-tooltip="true"
        /> -->
        <el-table-column align="center" label="学习人数" prop="studyCount">
        </el-table-column>
        <el-table-column align="center" label="状态" prop="enableStatus">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enableStatus"
              :active-value="1"
              :inactive-value="0"
              @change="changeStatus(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="platformType" label="发布平台">
        </el-table-column>
        <!-- <el-table-column
          align="center"
          prop="description"
          label="描述"
          :show-overflow-tooltip="true"
        >
        </el-table-column> -->
        <!-- <el-table-column align="center" label="内容" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-html="scope.row.content"></div>
          </template>
        </el-table-column> -->

        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              :disabled="scope.row.enableStatus == 1 ? true : false"
              @click="updateClick(scope.row)"
              >修改</el-button
            >
            <el-button type="text" @click="deleteLine(scope.row)"
              >删除</el-button
            >
            <el-button type="text" @click="details(scope.row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pageRight"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]"
        :current-page="searchForm.pageNum"
        :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { pickerOptions } from "./components/options";
import { isGreaterThanCurrentTime } from "@/utils/date";
import {getDictLabel} from "@/utils/dict";
import {
  subjectDeleteApi,
  subjectListApi,
  enableStatus,
} from "../../../../api/lesson";
export default {
  name: "Article",
  components: {},
  data() {
    return {
      props: {
        emitPath: false,
      },
      pickerOptions,
      str: "",
      searchForm: {
        categoryId: "",
        type: "",
        search: "",
        enableStatus: "",
        dateTime: "",
        pageNum: 1,
        pageSize: 10,
        platformType: ''
      },
      tableData: {
        data: [],
        total: 0,
      },
      listLoading: false,
      deleteList: [],
      cateList: [],
      linkAccountList: [],
      platformTypeList: [],
    };
  },
  async created() {
    this.platformTypeList = await this.$getDictData("platform_type");
    await this.getList();
  },
  mounted() {},
  methods: {
    handleClick(item) {
      console.log(item.menu);

      if (item.menu == 1) {
        this.$router.push({
          path: "/lessonMaterialadd/addlist",
        });
      }
    },
    // 删除专题
    deleteLine(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        subjectDeleteApi(row.id).then((res) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getList();
        });
      });
    },
    // 修改专题
    updateClick(row) {
      this.$router.push({
        path: "/lessonSubject/addlist",
        query: {
          id: row.id,
        },
      });
    },
    details(row) {
      this.$router.push({
        path: "/lessonSubject/details",
        query: {
          id: row.id,
        },
      });
    },

    changeStatus(v) {
      enableStatus({
        id: v.id,
        enableStatus: v.enableStatus,
      }).then((res) => {
        this.$message.success("操作成功");
        this.getList();
      });
    },
    // 重置
    resetForm(form) {
      this.searchForm = {
        categoryId: "",
        type: "",
        search: "",
        enableStatus: "",
        dateTime: "",
        pageNum: 1,
        pageSize: 10,
        platformType: ''
      };
      this.getList();
    },

    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getList();
    },
    addArtice() {
      this.$router.push({
        path: "/lessonSubject/addlist",
      });
    },
    getList() {
      this.listLoading = true;
      subjectListApi(this.searchForm)
        .then((res) => {
          res.data.list.forEach((item) => {
            console.log("item.platformType", item.platformType)
            item.platformType =
              item.platformType && getDictLabel(item.platformType, this.platformTypeList ).join(" , ");
          });
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.total;
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-dropdown {
  vertical-align: top;
}
.el-dropdown + .el-dropdown {
  margin-left: 15px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  position: relative;
}
/deep/.el-form--inline .el-form-item__label {
  line-height: 30px;
  padding-left: 15px;
  border-radius: 3px;
}
</style>
